﻿/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2010>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using MyPhotoIndex.Controls;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Interfaces.Imaging;
using System.Drawing;
using MyPhotoIndex.Plugins.Histogram.Properties;

namespace MyPhotoIndex.Plugins.Histogram
{
    class HistogramForm : AutoFadingForm
    {
        private PictureBox pictureBox1;
        private IApplicationContext m_applicationContext;
        private ComboBox HistogramTypeComboBox;
        private bool m_closing = false;
        private ReadOnlyCollection<int> m_selectedImages = null;
        private Image ms_mutipleimages_notice = Resources.multiple_images_notice;
        private Image ms_no_image_notice = Resources.no_image_notice;

        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.HistogramTypeComboBox = new System.Windows.Forms.ComboBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.Color.White;
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureBox1.Location = new System.Drawing.Point(1, 1);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(258, 100);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            // 
            // HistogramTypeComboBox
            // 
            this.HistogramTypeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.HistogramTypeComboBox.FormattingEnabled = true;
            this.HistogramTypeComboBox.Location = new System.Drawing.Point(1, 106);
            this.HistogramTypeComboBox.Name = "HistogramTypeComboBox";
            this.HistogramTypeComboBox.Size = new System.Drawing.Size(121, 21);
            this.HistogramTypeComboBox.TabIndex = 3;
            // 
            // HistogramForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(260, 133);
            this.Controls.Add(this.HistogramTypeComboBox);
            this.Controls.Add(this.pictureBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "HistogramForm";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Histogram";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        public HistogramForm(IApplicationContext applicationContext)
        {
            InitializeComponent();
            m_applicationContext = applicationContext;

            HistogramTypeComboBox.DataSource = Enum.GetValues(typeof(HistogramType));
            HistogramTypeComboBox.SelectedIndexChanged += new EventHandler(HistogramTypeComboBox_SelectedIndexChanged);
        }

        void HistogramTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DrawHistogram();
        }

        public void CloseForm()
        {
            m_closing = true;
            this.Close();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            this.Visible = false;

            if (m_closing == false)
            {
                e.Cancel = true;
            }
        }

        internal void SelectionChanged(ReadOnlyCollection<int> selectedImages)
        {
            m_selectedImages = selectedImages;
            DrawHistogram();
        }

        private void DrawHistogram()
        {
            if (pictureBox1.Image != null)
            {
                if (pictureBox1.Image != ms_mutipleimages_notice)
                {
                    pictureBox1.Image.Dispose();
                }

                pictureBox1.Image = null;
            }

            if (m_selectedImages.Count > 1)
            {
                pictureBox1.Image = ms_mutipleimages_notice;
                return;
            }

            if (m_selectedImages.Count == 0)
            {
                pictureBox1.Image = ms_no_image_notice;
                return;
            }

            if (m_selectedImages != null &&
                m_selectedImages.Count > 0)
            {
                int imageId = m_selectedImages[0];
                IImageData imageData = m_applicationContext.ImageCollection[imageId];

                HistogramProcessor hp = new HistogramProcessor();
                Image image = new Bitmap(imageData.Thumbnail);
                imageData.ThumbnailReferences++;
                imageData.ThumbnailReferences--;
                hp.setImage(image);
                pictureBox1.Image = hp.CalculateHistogram((HistogramType)HistogramTypeComboBox.SelectedValue);
            }
        }
    }
}
